class code4 {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if(root==null) return ret;
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);

        while(!q.isEmpty()) {
            int sz = q.size();
            int max = Integer.MIN_VALUE;
            for(int i=0;i<sz;i++) {
                TreeNode t = q.poll();
                max = Math.max(max,t.val);
                if(t.left!=null) q.add(t.left);
                if(t.right!=null) q.add(t.right);
            }
            ret.add(max);
        }
        return ret;
    }
}